package com.ecnu.web.controller.project;


import com.ecnu.common.constant.HttpStatus;
import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.system.domain.ProjectFile;
import com.ecnu.system.domain.ProjectFileLabels;
import com.ecnu.system.domain.ProjectIndividualFile;
import com.ecnu.system.domain.dto.FileDTO;
import com.ecnu.system.domain.dto.FileLabelDTO;
import com.ecnu.system.domain.dto.MyFileDTO;
import com.ecnu.system.domain.dto.ProjectFileDTO;
import com.ecnu.system.service.IProjectFileLabelsService;
import com.ecnu.system.service.IProjectFileService;
import com.ecnu.system.service.IProjectIndividualFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * ecnu_活动_资料表 前端控制器
 * </p>
 *
 * @author zdn
 * @since 2025-03-23
 */
@Api(tags = "活动文件管理")
@RestController
@RequestMapping("/project/file")
public class ProjectFileController extends BaseController {
    @Autowired
    private IProjectFileService projectFileService;
    @Autowired
    private IProjectFileLabelsService projectFileLabelsService;
    @Autowired
    private IProjectIndividualFileService projectIndividualFileService;

    /**
     * 查询活动资料列表
     *
     * @param filePram
     * @return
     */
    @ApiOperation(value = "1、查询活动资料列表")
    @PostMapping("/list")
    public TableDataInfo getList(@Valid @RequestBody FileDTO filePram) {
        try {
            return projectFileService.getList(filePram);
        } catch (Exception e) {
            logger.error("活动文件管理接口错误：\n" + e.getMessage());
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg(e.getMessage());
            return rspData;
        }

    }

    @ApiOperation(value = "1、删除活动资料")
    @PostMapping("/del")
    @PreAuthorize("@ss.hasPermi('project:file:del')")
    public AjaxResult delFile(@RequestBody String[] ids) {
        try {
            LoginUser loginUser = getLoginUser();
            List<ProjectFile> projects = Arrays.stream(ids)
                    .map(id -> {
                        ProjectFile file = new ProjectFile();
                        file.setId(id);
                        file.setDelFlag("2");
                        file.setUpdateUser(loginUser.getUserId().toString());
                        return file;
                    })
                    .collect(Collectors.toList());
            return projectFileService.updateBatchById(projects) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
        } catch (Exception e) {
            logger.error("活动删除错误" + e.getMessage());
            return AjaxResult.error("删除失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "1、编辑活动资料")
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('project:file:edit')")
    public AjaxResult editFile(@RequestBody ProjectFileDTO projectFile) {
        LoginUser user = getLoginUser();
        return projectFileService.editFile(projectFile, user);
    }
    @ApiOperation(value = "1、编辑活动资料（多选）")
    @PostMapping("/editList")
    @PreAuthorize("@ss.hasPermi('project:file:edit')")
    public AjaxResult editFileList(@RequestBody List<ProjectFileDTO> list) {
        LoginUser user = getLoginUser();
        list.forEach(projectFile -> projectFileService.editFile(projectFile, user));
        return AjaxResult.success("保存成功");
    }


    /**
     * 标签
     */
    @ApiOperation(value = "1.1、查询活动资料标签")
    @GetMapping("/labels/list")
    public AjaxResult getLabels(String id) {
        try {
            return projectFileLabelsService.getList(id);
        } catch (Exception e) {
            logger.error("活动文件标签查询错误：\n" + e.getMessage());
            return AjaxResult.error("活动文件标签查询错误：\n" + e.getMessage());
        }
    }

    @ApiOperation(value = "1.1、编辑活动资料标签")
    @PostMapping("/labels/edit")
    @PreAuthorize("@ss.hasPermi('project:labels:edit')")
    public AjaxResult editLabels(@RequestBody FileLabelDTO dtos) {
        try {
            return projectFileLabelsService.edit(dtos);
        } catch (Exception e) {
            logger.error("活动文件标签修改错误：\n" + e.getMessage());
            return AjaxResult.error("活动文件标签修改错误：\n" + e.getMessage());
        }
    }

    @ApiOperation(value = "1.1、编辑活动资料标签(多选)")
    @PostMapping("/labels/editList")
    @PreAuthorize("@ss.hasPermi('project:labels:editList')")
    public AjaxResult editLabels(@RequestBody List<FileLabelDTO> dtos) {
        try {
            return projectFileLabelsService.editList(dtos);
        } catch (Exception e) {
            logger.error("活动文件标签修改错误：\n" + e.getMessage());
            return AjaxResult.error("活动文件标签修改错误：\n" + e.getMessage());
        }
    }

    /**
     * 知识中心接口
     */

    @ApiOperation(value = "2、知识中心列表")
    @PostMapping("/myList")
    public TableDataInfo myList(@RequestBody MyFileDTO dto) {
        LoginUser loginUser = getLoginUser();
        return projectIndividualFileService.getList(dto, loginUser);
    }
    @ApiOperation(value = "2、知识中心删除")
    @PostMapping("/myDel")
    public AjaxResult myDel(@RequestBody String[] ids) {
        try {
            LoginUser loginUser = getLoginUser();
            List<ProjectIndividualFile> projects = Arrays.stream(ids)
                    .map(id -> {
                        ProjectIndividualFile file = new ProjectIndividualFile();
                        file.setId(id);
                        file.setDelFlag("2");
                        file.setUpdateUser(loginUser.getUserId().toString());
                        return file;
                    })
                    .collect(Collectors.toList());
            return projectIndividualFileService.updateBatchById(projects) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
        } catch (Exception e) {
            logger.error("活动删除错误" + e.getMessage());
            return AjaxResult.error("删除失败：" + e.getMessage());
        }
    }
    @ApiOperation(value = "2、知识中心编辑")
    @PostMapping("/myEdit")
    public AjaxResult myEdit(@RequestBody ProjectIndividualFile file) {
        LoginUser loginUser = getLoginUser();
        return projectIndividualFileService.edit(file, loginUser);
    }


    /**
     * 标签
     */
//    @ApiOperation(value = "1.1、查询活动资料标签")
//    @GetMapping("/labels/list")
//    public AjaxResult getLabels(String id) {
//        try {
//            return projectFileLabelsService.getList(id);
//        } catch (Exception e) {
//            logger.error("活动文件标签查询错误：\n" + e.getMessage());
//            return AjaxResult.error("活动文件标签查询错误：\n" + e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "1.1、编辑活动资料标签")
//    @PostMapping("/labels/edit")
//    @PreAuthorize("@ss.hasPermi('project:labels:edit')")
//    public AjaxResult editLabels(@RequestBody FileLabelDTO dtos) {
//        try {
//            return projectFileLabelsService.edit(dtos);
//        } catch (Exception e) {
//            logger.error("活动文件标签修改错误：\n" + e.getMessage());
//            return AjaxResult.error("活动文件标签修改错误：\n" + e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "1.1、编辑活动资料标签(多选)")
//    @PostMapping("/labels/editList")
//    @PreAuthorize("@ss.hasPermi('project:labels:editList')")
//    public AjaxResult editLabels(@RequestBody List<FileLabelDTO> dtos) {
//        try {
//            return projectFileLabelsService.editList(dtos);
//        } catch (Exception e) {
//            logger.error("活动文件标签修改错误：\n" + e.getMessage());
//            return AjaxResult.error("活动文件标签修改错误：\n" + e.getMessage());
//        }
//    }
}
